//
//  PingJiaVC.m
//  QoilPublic
//
//  Created by SunChao on 17/10/9.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "PingJiaVC.h"
#import "GlobalStarCell.h"
#import "DetailStarCell.h"
#import "EvaluateTextCell.h"
@interface PingJiaVC ()<UITableViewDelegate,UITableViewDataSource,StarSelectedDelegate,AdviseTextDelegate>
{
    NSString *_star1;
    NSString *_star2;
    NSString *_star3;
    NSString *_adviseString;
    NSArray *_textArr;
}

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIButton *judgeButton;

@end

@implementation PingJiaVC

- (id)init{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"评价";
    [self createTableView];
    [self gainStarText];
    
}


-(void)createTableView{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, kScreenWidth, kScreenHeight) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    _tableView.scrollEnabled = NO;
    [_tableView registerNib:[UINib nibWithNibName:@"GlobalStarCell" bundle:nil] forCellReuseIdentifier:@"GlobalStarCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"DetailStarCell" bundle:nil] forCellReuseIdentifier:@"DetailStarCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"MarkCell" bundle:nil] forCellReuseIdentifier:@"MarkCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"EvaluateTextCell" bundle:nil] forCellReuseIdentifier:@"EvaluateTextCell"];
    _judgeButton = [QViewUtility createButtonWithFrame:CGRectMake(0,kScreenHeight-50, kScreenWidth,50) font:17 title:@"提交" titleColor:[UIColor whiteColor] backgroundColor:[QColorUtility defaultBlueColor]];
    
    [_judgeButton blueStyle];
    
    [_judgeButton addTarget:self action:@selector(judgeButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_judgeButton];
}

#pragma mark--星级文字
- (void)gainStarText{
    
    NSString *url = [QParamService urlWithAction:@"order/getEvalType"];
    NSDictionary *params = [QParamService GainStarText];
   
    [[QHttpManager sharedManager] POST:url parameterMD5:params completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            
            _textArr = [basModel.items objectForKey:@"list"];
            [_tableView reloadData];
            
        }else{
            
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

#pragma mark--提交评价
- (void)judgeButtonAction{
    
    NSString *url = [QParamService urlWithAction:@"order/saveEvaluateOrder"];
    
    if (_star1 == nil || [_star1 isEqualToString:@""]) {
        _star1 = @"5";
    }
    if (_star2 == nil || [_star2 isEqualToString:@""]) {
        _star2 = @"5";
    }
    if (_star3 == nil || [_star3 isEqualToString:@""]) {
        _star3 = @"5";
    }
    
    NSArray *dic =  @[
                      @{
                          @"id" : @"1",
                          @"name" : @"服务态度",
                          @"stars" : _star1
                          },
                      @{
                          @"id" : @"2",
                          @"name" : @"配送速度",
                          @"stars" : _star2
                          },
                      @{
                          @"id" : @"3",
                          @"name" : @"油品质量",
                          @"stars" : _star3
                          }
                      ];
    NSData *data=[NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonStr=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"jsonStr==%@",jsonStr);
    //NSDictionary *dic = @{@"1":_star1,@"2":_star2,@"3":_star3};
    // NSString *str = [self dictionaryToJson:dic];
    NSDictionary *params = [QParamService CommentOrder:self.orderId content:_adviseString evalJsonstr:jsonStr];
    
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:params completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"评价成功" btnTitle:showAlertBtnTitle type:1 myBlock:^{
                [[NSNotificationCenter defaultCenter]postNotificationName:kNotificationCommentSuccess object:nil];
                if (_type == 1) {
                    [self.navigationController popToRootViewControllerAnimated:YES];
                }else{
                    [self.navigationController popViewControllerAnimated:YES];
                }
                
            }];
            
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

#pragma mark --- tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
   
    if (section == 0) {
        
        return 3;
    }
    
    if (section == 1) {
        return 1;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        NSMutableArray *arr = [NSMutableArray new];
        for (NSDictionary *tmpDic in _textArr) {
            NSString *content = [tmpDic objectForKey:@"content"];
            [arr addObject:content];
        }
        //arr = @[@"服务态度",@"配送速度",@"油品质量"];
        DetailStarCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailStarCell"];
        if (arr.count>0) {
            NSString *str  = [arr objectAtIndex:indexPath.row];
            [cell SetupStarViewWith:str indexPathOfRow:indexPath.row canEdit:YES starNum:@"" evaluate:@""];
        }
        
        cell.delegate = self;
        cell.tag = indexPath.row + 1;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    
    if (indexPath.section == 1) {
        EvaluateTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EvaluateTextCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        return 50;
    }
    if (indexPath.section == 1) {
        
        return 200;
    }
    
    return 0;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [[UIView alloc]init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
//字典转json格式字符串：
- (NSString*)dictionaryToJson:(id)dic
{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}


- (void)gainStarWithText: (NSString *) text indexPathOfRow:(NSUInteger )row{
    if (row == 0) {
        _star1 = text;
    }
    if (row == 1) {
        _star2 = text;
    }
    if (row == 2) {
        _star3 = text;
    }
}
-(void)gainStarWithText:(NSString *)text{
    
    _adviseString = text;
}
@end
